//
//  QXVisitLoginView.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/23.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit

protocol QXVisitLoginViewDelegate: NSObjectProtocol {
    func visitLoginViewDidClickLogin()
    func visitLoginViewDidClickRegister()
}

class QXVisitLoginView: UIView {
    
    func setupViewInfo(iconName: String, message: String, isHome: Bool) {
        messageLabel.text = message
        backIconView.hidden = !isHome;
        homeIconView.image = UIImage(named: iconName)
        homeIconView.sizeToFit()
        bringSubviewToFront(homeIconView)
    }
    
    weak var delegate: QXVisitLoginViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(backIconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        backgroundColor = UIColor(white: 237.0 / 256.0, alpha: 1.0)
        self.startAnimation()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 40
        anim.repeatCount = MAXFLOAT
        backIconView.layer.addAnimation(anim, forKey: "loginAnim1")
    }
    
    func stopAnimation() {
        backIconView.layer.removeAllAnimations()
    }
    
    func login() {
        delegate?.visitLoginViewDidClickLogin()
    }
    
    func register() {
        delegate?.visitLoginViewDidClickRegister()
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        backIconView.center = CGPointMake(self.center.x, 300)
        maskIconView.center = CGPointMake(self.center.x, 300)
        homeIconView.center = CGPointMake(self.center.x, 300)
        messageLabel.center = CGPointMake(self.center.x, 400)
        loginBtn.center = CGPointMake(self.center.x - 60, 450)
        registerBtn.center = CGPointMake(self.center.x + 60, 450)
    }
    
    // MARK: - 懒加载
    
    lazy var backIconView: UIImageView = {
        let img = UIImage(named: "visitordiscover_feed_image_smallicon")
        let imgV = UIImageView(image: img)
        self.addSubview(imgV)
        return imgV
    }()
    
    lazy var maskIconView: UIImageView = {
        let img = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imgV = UIImageView(image: img)
        return imgV
    }()
    
    lazy var homeIconView: UIImageView = {
        let img = UIImage(named: "visitordiscover_feed_image_house")
        let imgV = UIImageView(image: img)
        return imgV
    }()
    
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜！"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.bounds = CGRectMake(0, 0, 224, 40)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 2
        label.sizeToFit()
        return label
    }()
    
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.bounds = CGRectMake(0, 0, 100, 40)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.bounds = CGRectMake(0, 0, 100, 40)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    lazy var attentionBtn: UIButton = {
        let btn = UIButton()
        btn.bounds = CGRectMake(0, 0, 100, 40)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
